Skip to content

fix(session): return INVALID_JWT when JWKS lookup fails in authenticate - #785

Open
pcbeingused33 wants to merge 1 commit into
workos:mainfrom
pcbeingused33:fix/session-authenticate-jwks-error
Open

pcbeingused33 wants to merge 1 commit into
workos:mainfrom
pcbeingused33:fix/session-authenticate-jwks-error

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Description

Session.authenticate() and AsyncSession.authenticate() raise jwt.exceptions.PyJWKClientError when the JWKS lookup fails (for example, no signing key matches the token's kid, or the JWKS endpoint cannot be reached), instead of returning an AuthenticateWithSessionCookieErrorResponse.

Root cause: both methods wrap get_signing_key_from_jwt() and jwt.decode() in except jwt.exceptions.InvalidTokenError. PyJWKClientError derives from PyJWTError, not from InvalidTokenError, so it is not caught. refresh() (sync and async) already catches both and maps them to INVALID_JWT (added in #673); authenticate() was left out.

Fix: catch jwt.exceptions.PyJWKClientError alongside InvalidTokenError in both authenticate() methods, returning reason=INVALID_JWT, the same mapping refresh() uses. Only src/workos/session.py (hand-maintained, @oagen-ignore-file) is touched.

Tests: added test_session_authenticate_jwks_error_returns_invalid_jwt and test_async_session_authenticate_jwks_error_returns_invalid_jwt, mirroring the existing refresh test. Both fail on main with PyJWKClientError: Unable to find a signing key and pass with this change; tests/test_session.py passes in full (67 tests).

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

This PR was prepared with the help of an AI coding assistant (Claude Code).

Session.authenticate() and AsyncSession.authenticate() only caught
jwt.exceptions.InvalidTokenError, so a PyJWKClientError raised by the
JWKS lookup escaped to the caller. refresh() already maps that error to
INVALID_JWT; authenticate() now does the same.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@pcbeingused33
pcbeingused33 requested a review from a team as a code owner November 28, 2026 12:33
@greptile-apps

greptile-apps Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · This PR appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Both Session.authenticate() and AsyncSession.authenticate() now return INVALID_JWT when the signing-key lookup raises PyJWKClientError.

  • Session authentication returns INVALID_JWT when JWKS lookup fails.

pcbeingused333 explicitly chose INVALID_JWT for network failures to match refresh().

Reviews (1) · Last reviewed commit: "fix(session): return INVALID_JWT when JW..." · Reviewed by Greptile

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant